package dltic.job.job02;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * 二、给定以下代码，请定义方法listTest()统计集合中指定元素出现的次数，如"a": 2,"b": 2,"c" :1, "xxx":0。
 *
 */

public class Job02 {
	
	public static void main(String[] args) {
		
		Collection<String> list = new ArrayList<>();
		
        list.add("a");
        list.add("a");
        list.add("b");
        list.add("b");
        list.add("c");
        
        System.out.println("a:"+listTest(list, "a"));
        System.out.println("b:"+listTest(list, "b"));
        System.out.println("c:"+listTest(list, "c"));
        System.out.println("xxx:"+listTest(list, "xxx"));
    

	}

	private static String listTest(Collection<String> list, String string) {
		//定义统计变量
		int count = 0;
		//遍历集合
		Iterator<String> iterator = list.iterator();
		
		while(iterator.hasNext()){
			//让每个元素都能string比较  引用比较用equals
			if(iterator.next().equals(string)){
				count++;
			}
			
		}
		
		return String.valueOf(count);
	}

}
